# 本文件负责将请求解析为关键字，建立 请求<->关键字 的转化
# 简而言之就是接收请求🎈，触发函数
# get请求-->build-->返回
from django.shortcuts import render
from .views_bulid import *
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt



@csrf_exempt
def main(request):
    run_main(request)
    # print(json.loads(request.body))
    return HttpResponse('ok')


@csrf_exempt
def send_tip(request):
    # 时间一到就发起请求，收到请求发tip，请求为post
    tip_name = request.POST.get('name')
    temp_tip = tip.objects.filter(name=tip_name)
    if temp_tip.first():
        task = temp_tip.first()
        msg = title(task.name, time=int(datetime.timestamp(task.begin))) + totext(task.tips) + \
                (images(task.images.split(',')) if task.images else '')
        if task.method == 0 or task.method == 2: # 私人
            talks([j for i in user.objects.all().values('q_number') for j in i.values()], msg)
        if task.method == 0 or task.method == 1:  # 群发
            talks([i for i in default_info["default_group_number"]], ats(all='all')+msg, group=True)     # 缺设置提醒群聊
        drop_tip(tip_name)
    return HttpResponse('ok')


@csrf_exempt
def remind_task_interface(request):
    task_name = request.POST.get('name')
    last_time = request.POST.get('last_time')
    if task_name:
        res = remind_task(task_name)['info']
        talks(res, f'{task_name} 任务未提交！！！\n注意时间！！！\n剩余{last_time}！！！')
    return HttpResponse('ok')


@csrf_exempt
def hand_in_task(request):
    task_name = request.POST.get('name')
    if task_name:
        complete_task(task_name)
    talk(default_info["default_notice_q_number"], f"[{task_name}]任务截止，已自动提交！")
    return HttpResponse('ok')


@csrf_exempt
def help(request):
    return render(request, 'ReadMe.html')


@csrf_exempt
def _404(request):
    return HttpResponse('404')
